package com.easychat.mychat.userservice.Controller;

import com.easychat.mychat.common.Controller.ABaseController;

import com.easychat.mychat.common.Redis.RedisComponent;
import com.easychat.mychat.common.Redis.RedisUtils;
import com.easychat.mychat.common.entity.constants.Constants;
import com.easychat.mychat.common.entity.dto.MessageSendDto;
import com.easychat.mychat.common.entity.vo.ResponseVO;
import com.easychat.mychat.common.entity.vo.UserInfoVO;
import com.easychat.mychat.common.exception.BusinessException;
import com.easychat.mychat.common.webSocket.MessageHandler;
import com.easychat.mychat.common.webSocket.MessagePublisher;
import com.easychat.mychat.userservice.Service.UserInfoService;
import com.wf.captcha.ArithmeticCaptcha;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/4
 */
@RestController
@RequestMapping("/account")
@Validated
public class AccountController extends ABaseController {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private MessageHandler messageHandler;
    @Resource
    private MessagePublisher messagePublisher;

    @RequestMapping("/checkCode")
    public ResponseVO checkCode(){
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100,42);
        String text = captcha.text();
        String base64 = captcha.toBase64();
        String checkCodeKey = UUID.randomUUID().toString();
        redisUtils.setex(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey,text,Constants.REDIS_KEY_EXPIRES_ONE_MIN);
        Map<String,String> result = new HashMap<>();
        result.put("checkCode",base64);
        result.put("checkCodeKey",checkCodeKey);
        return getSuccessResponseVO(result);
    }

    @RequestMapping("/register")
    public ResponseVO register(@NotEmpty String checkCodeKey,
                               @NotEmpty @Email String email,
                               @NotEmpty String password,
                               @NotEmpty String nickName,
                               @NotEmpty String checkCode){
        //验证码
        String code = (String)redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        try {
            if (!code.equalsIgnoreCase(checkCode)) {
                throw new BusinessException("图片验证码不正确");
            }
            userInfoService.register(email, nickName, password);
            return getSuccessResponseVO(null);
        }finally {
            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

    }

    @RequestMapping("/login")
    public ResponseVO login(@NotEmpty String checkCodeKey,
                               @NotEmpty @Email String email,
                               @NotEmpty String password,
                               @NotEmpty String checkCode){
        String code = (String)redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        try {
            if (!code.equalsIgnoreCase(checkCode)) {
                throw new BusinessException("图片验证码不正确");
            }
            UserInfoVO login = userInfoService.login(email, password);
            return getSuccessResponseVO(login);
        }finally {
            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }
    }

    @RequestMapping("/getSysSetting")
    public ResponseVO getSysSetting(){
        return getSuccessResponseVO(redisComponent.getSysSetting());
    }

    @RequestMapping("/test")
    public ResponseVO test(){
        MessageSendDto messageSendDto = new MessageSendDto();
        messageSendDto.setMessageContent("aaaaa" + System.currentTimeMillis());
//        messageHandler.sendMessage(messageSendDto);
        messagePublisher.sendMsg(messageSendDto);
        return getSuccessResponseVO(null);
    }
}
